package com.example.administrator.shzs.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.activity.BaseActivity;
import com.example.administrator.shzs.adapter.CollectShopListAdapter;
import com.example.administrator.shzs.adapter.HomeShopListAdapter;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.entity.CollectShopData;
import com.example.administrator.shzs.entity.CollectShopData.ListBean;
import com.example.administrator.shzs.fragment.HomeFragment;
import com.example.administrator.shzs.parse.ParseCollectShopList;
import com.example.administrator.shzs.url.Contents;
import com.example.administrator.shzs.utils.ToastUtils;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 关注的商家店铺
 */
public class AttentionShopActivity extends BaseActivity implements View.OnClickListener {


    @Bind(R.id.mXecyclerView)
    XRecyclerView mXRecyclerView;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_not_collect_shop)
    TextView tvNotCollectShop;

    private List<ListBean> listBeens;
    //private List<ListBean> mMoreData;

    private CollectShopListAdapter shopListAdapter;

    private int page=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attention_shop);
        ButterKnife.bind(this);
        MyApplication.app.activityList.add(this);
        setXRecyclerView();



        ivBack.setOnClickListener(this);
        refresh();
    }


    @Override
    protected void onResume() {
        super.onResume();
        getDataFormServer(1);
    }

    /**
     * 刷新
     */
    private void refresh() {


        mXRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getDataFormServer(1);
                        mXRecyclerView.refreshComplete();
                    }
                },500);
            }

            @Override
            public void onLoadMore() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        /*page++;
                        getDataFormServer(page);*/
                        mXRecyclerView.loadMoreComplete();
                    }
                },500);
            }
        });
    }

    private void getDataFormServer(final int page) {

        String url = Contents.COLLECT_SHOP_LIST_URL + "pagesize=20&pageindex=" + page + "&userid=" + MyApplication.app.getUserId();

        RequestParams params = new RequestParams(url);
        Log.e("Life","收藏的店家"+params.toString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {

                    if (page == 1) {
                        listBeens = ParseCollectShopList.getCollectShopList(result);
                        if(listBeens.size()==0) {
                            mXRecyclerView.setVisibility(View.GONE);
                            tvNotCollectShop.setVisibility(View.VISIBLE);
                        }
                        setAdapter(listBeens);
                    } else {//暂时不做
                        /*mMoreData = ParseCollectShopList.getCollectShopList(result);
                        if (mMoreData.size() == 0) {
                            ToastUtils.showMessageShort(AttentionShopActivity.this,"没有更多了哦");
                            return;
                        }
                        listBeens.addAll(mMoreData);
                        mMoreData.clear();
                        shopListAdapter.notifyDataSetChanged();*/
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });

    }


    /**
     * 设置Adapter
     */
    private void setAdapter(List<ListBean> listBeens) {

        shopListAdapter=new CollectShopListAdapter(this,listBeens);
        mXRecyclerView.setAdapter(shopListAdapter);

        setListeners();

    }

    /**
     * 初始化
     */
    private void setXRecyclerView() {

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayout.VERTICAL);
        mXRecyclerView.setLayoutManager(manager);

        //设置可刷新
        mXRecyclerView.setRefreshing(true);
        mXRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);//刷新样式
        mXRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.Pacman);//加载更多样式
    }

    /**
     * 监听器
     */
    private void setListeners() {

        shopListAdapter.setOnItemClickListener(new CollectShopListAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(ListBean datalist) {

                ListBean bean=datalist;
                Intent shopActivity=new Intent(AttentionShopActivity.this,ShopActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("ListBean",bean);
                shopActivity.putExtras(bundle);
                shopActivity.putExtra("AttentionShopActivity",true);
                startActivity(shopActivity);
            }

            @Override
            public void onShopPayClick(ListBean datalist) {
                //到店付
                Intent offlinePay = new Intent(AttentionShopActivity.this, OfflinePayActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("ListBean", datalist);
                offlinePay.putExtras(bundle);
                offlinePay.putExtra("AttentionShopActivity",true);
                startActivity(offlinePay);
            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.iv_back:

                finish();

                break;

        }

    }
}
